package edu.cmu.cs.webapp.ioms;

import org.eclipse.jface.window.Window;
import org.eclipse.rwt.RWT;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchAdvisor;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

import edu.cmu.cs.webapp.ioms.perspectives.OrderManagementPerspective;
import edu.cmu.cs.webapp.ioms.ui.dialogs.LoginDialog;

/**
 * This workbench advisor creates the window advisor, and specifies the
 * perspective id for the initial window.
 */
public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {

	public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(
			IWorkbenchWindowConfigurer configurer) {
		return new ApplicationWorkbenchWindowAdvisor(configurer);
	}

	@Override
	public void preStartup() {
		System.out.println(RWT.getSessionStore().getId());
		// check if the user has already login
		if (RWT.getSessionStore().getAttribute("uid") == null) {
			LoginDialog dlg = new LoginDialog(getWorkbenchConfigurer()
					.getWorkbench().getDisplay().getActiveShell(), true);

			if (dlg.open() != Window.OK) {
				// closing the workbench if verification error
				getWorkbenchConfigurer().getWorkbench().close();
			}
		}

	}

	public String getInitialWindowPerspectiveId() {
		return OrderManagementPerspective.PERSPECTIVE_ID;
	}
}
